require("../less/sportDate.less");
require("../lib/iconfont/iconfont.css");
document.ready(function () {
    let userId = window.$utils.istongguo();
    /* 点击跳转 */
    document.querySelector("#lcMy").addEventListener("click", function () {
        location.href = "my.html"
    })

    /* 图表 */

    function hellotubiao1(xmd, ymd) {

        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.getElementById('chart'));

        // 指定图表的配置项和数据
        var option = {
            title: {
                text: '近7天运动时长'
            },
            tooltip: {

            },
            legend: {
                data: ['运动时间']
            },
            xAxis: {

                data: xmd
            },
            yAxis: {},
            series: [{
                name: '运动时间',
                type: 'bar',
                data: ymd
            }]
        };
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
    }
   
    function hellotubiao2(arr) {

        var chartDom = document.getElementById('chart2');
        var myChart = echarts.init(chartDom);
        var option;

        option = {
            title: {
                text: '运动数据',

            },
            tooltip: {
                trigger: 'item'
            },
            legend: {
                orient: 'vertical',
                left: 'left',
                top: "middle",
                itemWidth: 10,
                itesHight: 0,
            },
            series: [
                {
                    name: '时长',
                    type: 'pie',
                    radius: '50%',

                    data: arr,

                      
                    
                    emphasis: {
                        itemStyle: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }, label: {
                        position: "inside",
                        formatter: "{d}%"
                    }
                }
            ]
        };

        option && myChart.setOption(option);
    }

    /* 配置ajax */
    window.$http.get("/sport/data/" + userId, function (res) {
        if (res.status === 0) {
            let data = res.data;
            console.log(data)
            let xmd = data.days.map(function (v) {
                return v.date.substr(5)
            })

            let ymd = data.days.map(function (v) {
                return v.dayTimes
            })

            let index = 0;
            let arr3 = ["red","yellow","green"]
            hellotubiao1(xmd, ymd)

                let arr1 = []
          let obj = {
                "跑步":0,
                "骑行":0,
                "训练":0,
            } 
         let data1=data.days.forEach(function(v){
                v.exerciseData.forEach(function(item){
                    arr1.push(item)
                  
                })
            }) 
          arr1.forEach(function(vaule){
              obj[vaule.type]+=vaule.time*1
          })

          let arr2 = [];


          for(let key in obj){
             console.log(key,obj[key])
             arr2.push({value:obj[key],name:key,color:arr3[index]})
             index++
          }

          
         hellotubiao2(arr2)
            /*             { value: 735, name: '跑步' },
                        { value: 580, name: '骑行' },
                        { value: 484, name: '训练' },
             */

             /* 渲染上面的数据 */
             document.querySelector("#continueDays").textContent = data.continueDays+"天"
             document.querySelector("#sumDays").textContent = data.sumDays+"天";
             document.querySelector("#sumTimes").textContent = data.sumTimes;

        }

        /* 配置本周消耗以及卡路里ajax */
        window.$http.get("/sports/exerciseData?id="+userId,function(res){
            document.querySelector("#calorie").textContent = Math.floor(res.data.calorie);
            document.querySelector("#times").textContent = Math.floor(res.data.times);
        })

        /* 配置头像 */
        window.$http.get("/users/accountinfo?userId="+userId,function(res){
            document.querySelector("#getImg").src = "http://139.9.177.51:8099"+ res.data.imgurl
        })
    })













})